﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace Weazel.Badger.Vhdl.Types
{
  public abstract class Type
  {
    private TypeId type;
    public TypeId TypeId
    {
      get
      {
        return type;
      }

      set
      {
        type = value;
      }
    }

		/// <summary>
		/// Returns the width of this type
		/// </summary>
    private int width;
    public int Width
    {
      get
      {
        return width;
      }
    }

		/// <summary>
		/// Create a new type
		/// </summary>
		/// <param name="type">TypeId</param>
		/// <param name="width">width of type</param>
    public Type(TypeId type, int width)
    {
      this.type = type;
      this.width = width;
    }

		/// <summary>
		/// Must overload.
		/// </summary>
		/// <returns></returns>
    public override int GetHashCode()
    {
      return type.Identifier;
    }

		/// <summary>
		/// Compare this type with another object
		/// </summary>
		/// <param name="obj">object to compare with</param>
		/// <returns></returns>
    public override bool Equals(object obj)
    {
      if (obj is Type)
      {
        return type == ((Type)obj).type;
      }
      else if(obj is TypeId)
      {
        return type == (TypeId)obj;
      }

      return false;      
    }

    public virtual bool Compare(Type t)
    {
      return type == t.TypeId;
    }

    public static bool operator ==(Type left, object right)
    {
      if (right is Type)
        return left == (Type)right;
      else if (right is TypeId)
        return left == (TypeId)right;
      else
        return false;
    }

    public static bool operator !=(Type left, object right)
    {
      return !(left == right);
    }

    public static bool operator== (Type left, Type right)
    {
      return left.Compare(right);
    }

    public static bool operator ==(Type left, TypeId right)
    {
      return left.type == right;
    }

    public static bool operator!= (Type left, Type right)
    {
      return !left.Compare(right);
    }

    public static bool operator !=(Type left, TypeId right)
    {
      return left.type != right;
    }

    public override string ToString()
    {
      return type.name;
    }
  }
}
